import router from './router'
import store from './store'
// 进度条插件 https://github.com/rstacruz/nprogress
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import getPageTitle from '@/utils/get-page-title'
import { asyncRoutes } from '@/router/index'
NProgress.configure({ showSpinner: false })
const whiteList = ['/login', '/404'] // 白名单

router.beforeEach(async(to, from, next) => {
  // 页面开始跳转启动进度条
  NProgress.start() // 启动进度条
  const token = store.state.user.token
  // 有token
  if (token) {
    // 去登录
    if (to.path === '/login') {
      // 回主页
      // 不是正常走next() 手动结束进度条
      next('/')
      NProgress.done()
    } else {
      // 不去登录页
      // 调用actions中的函数
      const userId = store.state.user.userInfo.userId
      if (!userId) {
        // vuex的user模块actions的返回值
        const res = await store.dispatch('user/getUserInfo')
        // 过滤出用户可以访问的路由配置
        const filterRouter = asyncRoutes.filter(ele => {
          return res.roles.menus.includes(ele.children[0].name)
        })
        // 让404在整个路由配置的最后
        filterRouter.push({ path: '*', redirect: '/404', hidden: true })
        // 动态添加路由配置
        router.addRoutes(filterRouter)
        // 通过vuex储存路由配置数组，渲染左侧菜单栏
        store.commit('menu/setMenus', filterRouter)
        // 解决白屏的问题
        next({
          ...to,
          replace: true
        })
      } else {
        // 不值钱的两行代码
        if (from.path === '/login' && to.path === '/404') {
          next('/')
        }
        next()
      }
    }
  } else {
    // 没有token
    // arr.includes(元素) 判断元素是否在数组中 返回值Boolean
    if (whiteList.includes(to.path)) { // 是否访问白名单
      next() // 放行
    } else {
      // 不去登录
      next('/login') // 跳转登录页
      // 不是正常走next() 手动结束进度条
      NProgress.done()
    }
  }
})
// 路由守卫可以写多个
router.beforeEach((to, from, next) => {
  // 动态设置页面标题
  document.title = getPageTitle(to.meta.title)
  //
  next()
})
router.afterEach((to, form) => {
  NProgress.done() // 进度条结束
})
// import { Message } from 'element-ui'
// import NProgress from 'nprogress' // progress bar
// import 'nprogress/nprogress.css' // progress bar style
// import { getToken } from '@/utils/auth' // get token from cookie
// import getPageTitle from '@/utils/get-page-title'

// NProgress.configure({ showSpinner: false }) // NProgress Configuration

// const whiteList = ['/login'] // no redirect whitelist

// router.beforeEach(async(to, from, next) => {
//   // start progress bar
//   NProgress.start()

//   // set page title
//   document.title = getPageTitle(to.meta.title)

//   // determine whether the user has logged in
//   const hasToken = getToken()

//   if (hasToken) {
//     if (to.path === '/login') {
//       // if is logged in, redirect to the home page
//       next({ path: '/' })
//       NProgress.done()
//     } else {
//       const hasGetUserInfo = store.getters.name
//       if (hasGetUserInfo) {
//         next()
//       } else {
//         try {
//           // get user info
//           await store.dispatch('user/getInfo')

//           next()
//         } catch (error) {
//           // remove token and go to login page to re-login
//           await store.dispatch('user/resetToken')
//           Message.error(error || 'Has Error')
//           next(`/login?redirect=${to.path}`)
//           NProgress.done()
//         }
//       }
//     }
//   } else {
//     /* has no token*/

//     if (whiteList.indexOf(to.path) !== -1) {
//       // in the free login whitelist, go directly
//       next()
//     } else {
//       // other pages that do not have permission to access are redirected to the login page.
//       next(`/login?redirect=${to.path}`)
//       NProgress.done()
//     }
//   }
// })

// router.afterEach(() => {
//   // finish progress bar
//   NProgress.done()
// })
